package testes;

import static org.junit.Assert.*;

import iscte.es.copaPaises.businessLayer.CalendarDate;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class CalendarDateTest {
	protected CalendarDate d1;
	protected CalendarDate d2;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCalendarDate() {

		assertFalse("Date already exists",CalendarDate.allInstances.contains(d1));
		d1=new CalendarDate();
		assertTrue("CalendarDate()",CalendarDate.allInstances.contains(d1));
		d1.destroy();
	}

	@Test
	public void testCalendarDateIntIntInt() {

		d1=new CalendarDate(23, 4, 2009);
		assertTrue("CalendarDate(dia,mes,ano)",CalendarDate.allInstances.contains(d1));
		d1.destroy();
	}

	@Test
	public void testDestroy() {
		d1=new CalendarDate();
		d1.destroy();
		assertFalse("CalendarDate.destroy()",CalendarDate.allInstances.contains(d1));
	}

	@Test
	public void testDay() {
		d1=new CalendarDate();
		int day=1;

		d1.setDay(day);
		assertNotSame("Calendar.setDay() setter ",0,d1.day());
		assertEquals("CalendarDate.day() getter ",day,d1.day());
		d1.destroy();
	}

	@Test
	public void testMonth() {
		d1=new CalendarDate();
		int month=1;

		d1.setMonth(month);
		assertNotSame("Calendar.setMonth() setter ",0,d1.month());
		assertEquals("CalendarDate.month() getter  ",month,d1.month());
		d1.destroy();

	}

	@Test
	public void testYear() {
		d1=new CalendarDate();
		int year=1;
		d1.setYear(year);
		assertNotSame("Setter Calendar.setYear() ",0,d1.year());
		assertEquals("Getter CalendarDate.month() ",year,d1.year());
		d1.destroy();

	}

	@Test
	public void testInitS() {
		int day=9, month=1,year=2012;
		d1=new CalendarDate();
		d1.initS(year+"-0"+month+"-0"+day);
		assertTrue("CalendarDate.InitS()",CalendarDate.allInstances.contains(d1));
		assertEquals("CalendarDate.InitS()",day,d1.day());
		assertEquals("CalendarDate.InitS()",month,d1.month());
		assertEquals("CalendarDate.InitS()",year,d1.year());
		d1.destroy();
	}

	@Test
	public void testIsAfter() {
		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(24, 4, 2009);
		assertTrue("CalendarDate.isAfter()",d2.isAfter(d1));
		assertFalse("CalendarDate.isAfter()",d1.isAfter(d2));
		d1.destroy();
		d2.destroy();

		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(23, 5, 2009);
		assertTrue("CalendarDate.isAfter()",d2.isAfter(d1));
		assertFalse("CalendarDate.isAfter()",d1.isAfter(d2));
		d1.destroy();
		d2.destroy();

		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(24, 5, 2010);
		assertTrue("CalendarDate.isAfter()",d2.isAfter(d1));
		assertFalse("CalendarDate.isAfter()",d1.isAfter(d2));
		d1.destroy();
		d2.destroy();	
	}

	@Test
	public void testIsBefore() {
		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(24, 4, 2009);
		assertFalse("CalendarDate.isBefore()",d2.isBefore(d1));
		assertTrue("CalendarDate.isBefore()",d1.isBefore(d2));
		d1.destroy();
		d2.destroy();

		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(23, 5, 2009);
		assertFalse("CalendarDate.isBefore()",d2.isBefore(d1));
		assertTrue("CalendarDate.isBefore()",d1.isBefore(d2));
		d1.destroy();
		d2.destroy();

		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(24, 5, 2010);
		assertFalse("CalendarDate.isBefore()",d2.isBefore(d1));
		assertTrue("CalendarDate.isBefore()",d1.isBefore(d2));
		d1.destroy();
		d2.destroy();
	}

	@Test
	public void testIsDivisible() {
		d1=new CalendarDate();
		d1.isDivisible(1, 2);
		assertFalse("CalendarDate.isDivisible",d1.isDivisible(1, 2));
		assertTrue("CalendarDate.isDivisible",d1.isDivisible(4, 2));
		d1.destroy();
	}

	@Test
	public void testIsEqual() {
		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(23, 4, 2009);
		assertTrue("CalendarDate.isEqual()",d1.isEqual(d2));

		d2.destroy();
		d2=new CalendarDate(24, 4, 2009);
		assertFalse("CalendarDate.isEqual()",d1.isEqual(d2));
		d2.destroy();
		d2=new CalendarDate(23, 5, 2009);
		assertFalse("CalendarDate.isEqual()",d1.isEqual(d2));
		d2.destroy();
		d2=new CalendarDate(23, 4, 2010);
		assertFalse("CalendarDate.isEqual()",d1.isEqual(d2));

		d1.destroy();
		d2.destroy();
	}

	@Test
	public void testIsLeap() {
		d1=new CalendarDate(23, 4, 2012);
		assertTrue("CalendarDate.isLeap()",d1.isLeap());
		d1.destroy();
		d1=new CalendarDate(23, 4, 100);
		assertFalse("CalendarDate.isLeap()",d1.isLeap());
		d1.destroy();
		d1=new CalendarDate(23, 4, 400);
		assertTrue("CalendarDate.isLeap()",d1.isLeap());
		d1.destroy();
		d1=new CalendarDate(23, 4, 2013);
		assertFalse("CalendarDate.isLeap()",d1.isLeap());


	}

	@Test
	public void testStringToDate() {
		d1=new CalendarDate();
		int day=9, month=6,year=2012;
		d1=d1.stringToDate(year+"-0"+month+"-0"+day);
		assertEquals("CalendarDate.StringToDate()",d1.day(),day);
		assertEquals("CalendarDate.StringToDate()",d1.month(),month);
		assertEquals("CalendarDate.StringToDate()",d1.year(),year);

	
		d1=d1.stringToDate(year+"-0"+month+"-0"+day);
		assertEquals("CalendarDate.StringToDate()",d1.day(),day);
		assertEquals("CalendarDate.StringToDate()",d1.month(),month);
		assertEquals("CalendarDate.StringToDate()",d1.year(),year);

		day=8;
		month=6;
		year=2012;
		d2=new CalendarDate();
		d2=d2.stringToDate(year+"-0"+month+"-0"+day);
		assertEquals("CalendarDate.StringToDate()",d2.day(),day);
		assertEquals("CalendarDate.StringToDate()",d2.month(),month);
		assertEquals("CalendarDate.StringToDate()",d2.year(),year);
		
		CalendarDate d3=new CalendarDate();
		day=8;
		month=5;
		year=2012;
		d3=new CalendarDate();
		d3=d3.stringToDate(year+"-0"+month+"-0"+day);
		assertEquals("CalendarDate.StringToDate()",d3.day(),day);
		assertEquals("CalendarDate.StringToDate()",d3.month(),month);
		assertEquals("CalendarDate.StringToDate()",d3.year(),year);
		
		d3.destroy();
		d2.destroy();
		d1.destroy();



	}

	@Test
	public void testToString() {
		d1=new CalendarDate();
		String date_S="1990-02-03";
		
		d1.initS(date_S);
		assertEquals("CalendarDate.toString()",d1.toString(),date_S);
		
		date_S="1990-12-13";
		d1.initS(date_S);
		assertEquals("CalendarDate.toString()",d1.toString(),date_S);
		
		d1.destroy();
		
	}

	@Test
	public void testYearsSince() {
		
		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(23, 5, 2010);
		assertEquals("CalendarDate.yearsSince()", d2.yearsSince(d1),1);
		
		d1.destroy();
		d2.destroy();
		
		//this.month==t.month && this.day<t.day
		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(22, 4, 2012);
		assertEquals("CalendarDate.yearsSince()",d2.yearsSince(d1),2);
		
		d1.destroy();
		d2.destroy();
		
		d1=new CalendarDate(23, 4, 2009);
		d2=new CalendarDate(23, 3, 2010);
		assertEquals("CalendarDate.yearsSince()",d2.yearsSince(d1),0);
		
		d1.destroy();
		d2.destroy();
		
		d1=new CalendarDate(23, 5, 2009);
		d2=new CalendarDate(23, 5, 2010);
		assertEquals(d2.yearsSince(d1),1);
		
		d1.destroy();
		d2.destroy();
		
		
	}

}
